package com.foreknow.ex.ex8;

import java.util.Scanner;

/**
 * Created by foreknow on 2021/7/25.
 */
public class Test {
    /**
     * 输出图书信息
     *
     * @param
     */
    public void printBooksInfo(Book[] books) {
        books[0] = new Book(1, "java", 33.45, 50);
        books[1] = new Book(2, "jsp", 55.45, 150);
        books[2] = new Book(3, "ssm", 77.45, 250);

        for (int i = 0; i < books.length; i++) {
            Book book = books[i];
            System.out.println(book.getBid() + "\t" + book.getBname() + "\t"
                    + book.getPrice() + "\t" + book.getStorage());
        }
    }

    /**
     * 购买图书
     *
     * @param
     */
    public Order buyBooks(Book[] books) {
        Scanner input = new Scanner(System.in);
        // 1.创建一个订单（手动给一个订单编号）
        Order order = new Order("0001");
        // 2.循环3次接收用户输入
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入图书的编号：");
            int pid = input.nextInt();
            System.out.println("请输入购买数量:");
            int pnum = input.nextInt();
            // 3.将购买的商品保存到数组中
            OrderItem item = new OrderItem(books[pid - 1].getBname(),
                    books[pid - 1].getPrice(), pnum);
            order.setItem(item, i);
            System.out.println("请继续购买图书");
        }
        return order;
    }

    /**
     * 获取订单信息
     *
     * @param
     */

//    public void printOrderInfo(Order order) {
//
//    }

    public static void main(String[] args) {

        Test t = new Test();
        Book[] books = new Book[3];
        // 1.输出图书信息
         t.printBooksInfo(books);

        // 2.购买图书
        books[0] = new Book(1, "java", 33.45, 50);
        books[1] = new Book(2, "jsp", 55.45, 150);
        books[2] = new Book(3, "ssm", 77.45, 250);

        Order order = t.buyBooks(books);


        // 3.输出订单信息
        // 3.1输出订单的编号
        System.out.println(order.getOrderId());
        // 3.2输出订单的总额
        System.out.println("订单的总额为" + order.getTotal());

        // 3.3输出商品列表中的内容
        OrderItem[] itt = order.getItems();
        for(int i = 0;i<itt.length;i++) {
            OrderItem io = itt[i];
            System.out.println(io.getPname()+"--"+io.getPprice()+"--"+io.getNum());
        }


    }
}
